package com.huilian.iotbox.admin.wap.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.wap.dao.CommodityInfoCommonDao;
import com.huilian.iotbox.admin.wap.service.CommodityInfoCommonService;
import com.huilian.iotbox.admin.wap.service.CommodityService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.po.CommodityInfoCommon;
import com.huilian.iotbox.data.service.UploadService;
import com.huilian.iotbox.data.vo.CommodityInfoCommonVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 谢洛涛
 * @date 2023/9/7 20:20
 */
@Service
@Slf4j
public class CommodityInfoCommonServiceImpl implements CommodityInfoCommonService {
    @Resource
    private CommodityInfoCommonDao commodityInfoCommonDao;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private UploadService uploadService;
    @Override
    public PageInfo<CommodityInfoCommonVo> findCommodityInfCommon(CommodityInfoCommon commodityInfoCommon) {
        PageHelper.startPage(commodityInfoCommon.getPageNum(), commodityInfoCommon.getPageSize());
        List<CommodityInfoCommonVo> list = new ArrayList<>();
        // 目前公有的条形码查询只会查询出一个，所以也把私有的也查询出来，这样查询可能回漏掉一两个，但没关系
        if(!StringUtils.isEmpty(commodityInfoCommon.getBarCode())){
            if(commodityInfoCommon.getPageNum() == 1) {
                list = commodityInfoCommonDao.findCommodityInfCommon(commodityInfoCommon);
            }
            List<CommodityInfoCommonVo> commodityList =  commodityService.findCommodityByBarCode(commodityInfoCommon.getBarCode());
            list.addAll(commodityList);
        }else{
            list =  commodityInfoCommonDao.findCommodityInfCommon(commodityInfoCommon);
        }
        List<CommodityInfoCommonVo> newList = new ArrayList<>();
        if(list.size()>0){
            newList.add(list.get(0));
        }
        return new PageInfo<>(newList);
    }

    @Override
    public ServerResponse findOneCommodityInfoCommon(CommodityInfoCommon commodityInfoCommon) {
        CommodityInfoCommonVo oneCommodityInfoCommon = commodityInfoCommonDao.findOneCommodityInfoCommon(commodityInfoCommon);
        if(ObjectUtils.isEmpty(oneCommodityInfoCommon)){
            return ServerResponse.createByErrorMessage("该商品不存在");
        }
        //把公共库网络图片上传到服务器了
        try {
            String imageUrl = "https://static.iotbox.huilian.cloud/images"+oneCommodityInfoCommon.getFileName();
            log.info("网络图片完整地址："+imageUrl);
            URL url = new URL(imageUrl);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            log.info("inputStream："+inputStream);
            // 使用inputStream处理图片数据
             ServerResponse serverResponse = uploadService.uploadImageToFTP(inputStream);
             Map<String,String> data =( Map<String,String>) serverResponse.getData();
             log.info("data："+data);
            final String fileName = data.get("keyRes");
            log.info("图片前缀："+fileName);
            oneCommodityInfoCommon.setFileName(fileName);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ServerResponse.createBySuccess(oneCommodityInfoCommon) ;
    }
}
